package com.hd.trans.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;

import com.hd.trans.R;
import com.hd.trans.framework.rom.RomUtils;
import com.hd.trans.network.PreferenceMgr;
import com.hd.trans.ui.base.AppContextProvider;
import com.hd.trans.ui.service.SoundRecordingService;

public class NotificationUtil {

    public void hideMicrophoneNotification() {
        Context context = AppContextProvider.getContext();
        context.stopService(new Intent(context,SoundRecordingService.class));
    }

    public void showMicrophoneNotification() {
        showMicrophoneNotification(null);
    }

    public void showMicrophoneNotification(@Nullable Runnable callback) {
        SoundRecordingService.start(AppContextProvider.getContext(), callback);
    }

    public boolean isNotificationEnable() {
        NotificationManagerCompat notification = NotificationManagerCompat.from(AppContextProvider.getContext());
        return notification.areNotificationsEnabled();
    }

    public void checkNotification(Context context) {
        if (RomUtils.checkIsHuaweiRom()) {
            return;
        }
        try {
            if (PreferenceMgr.getInstance().isNotificationDialogEnable() && !isNotificationEnable()) {
                showNotificationDialog(context);
                PreferenceMgr.getInstance().saveNotificationDialogTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void openNotificationSetting(Context context) {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE",context.getPackageName());
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  //5.0
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", context.getPackageName());
            intent.putExtra("app_uid", context.getApplicationInfo().uid);
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {  //4.4
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
        } else if (Build.VERSION.SDK_INT >= 15) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        }
        context.startActivity(intent);
    }

    public void showNotificationDialog(Context context) {
        final Dialog myDialog = new Dialog(context, R.style.BottomDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.permission_tip_layout, (ViewGroup) null);
        LinearLayout commonLayout = view.findViewById(R.id.common);
        TextView titleTv = view.findViewById(R.id.common_title_tv);
        TextView content = view.findViewById(R.id.common_content_tv);
        commonLayout.setVisibility(View.VISIBLE);
        titleTv.setText(context.getString(R.string.notification_permission));
        content.setText(context.getString(R.string.notification_desc));
        TextView sure = view.findViewById(R.id.retry);
        ImageView close = view.findViewById(R.id.close);
        sure.setOnClickListener(v -> {
            myDialog.dismiss();
            openNotificationSetting(context);
        });
        close.setOnClickListener(v -> myDialog.dismiss());

        myDialog.setContentView(view);
        myDialog.show();
        Window window = myDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        Resources var10001 = Resources.getSystem();
        lp.width = (int) (var10001.getDisplayMetrics().widthPixels * 0.9);
        Window var10000 = myDialog.getWindow();
        var10000.setAttributes(lp);
    }


    private static class Holder {
        private static NotificationUtil instant = new NotificationUtil();
    }

    public static NotificationUtil getInstance() {
        return Holder.instant;
    }
}